<?php

namespace App\Http\Requests;

use App\Models\Orders;
use App\Models\OrdersGoods;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request;

class OrdersCommentAdd extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(Request $request)
    {
        return [
            'comment_info' => [
                'required', function($attribute, $value, $fail) use ($request) {
                    if (!is_array($value)) {
                        $fail('评论参数错误！！！');
                        return;
                    }

                    try {
                        foreach ($value as $item) {
                            $ordersGoods = OrdersGoods::whereUserId(auth('wechat')->id())
                                ->where('orders_id', $request->post('orders_id'))
                                ->where('goods_id', $item['goods_id'])
                                ->first();
                            if (empty($ordersGoods)) {
                                $fail('订单商品不存在');
                                return;
                            }

                            if (strlen($item['content']) < 1) {
                                $fail('请输入评论内容');
                                return;
                            }
                            if (strlen($item['content']) > 100) {
                                $fail('评论内容不能大于100个字');
                                return;
                            }
                            if (!in_array($item['score'], [1,2,3,4,5])) {
                                $fail('评分不符合规定');
                                return;
                            }
                        }
                    } catch (\Exception $exception) {
                        \Log::error($exception);
                        $fail('评论参数错误？？？');
                        return;
                    }
                }
            ],
            'orders_id' => [
                'required', function($attribute, $value, $fail) {
                    $orders = Orders::whereUserId(auth('wechat')->id())
                        ->where('id', $value)
                        ->first();
                    if (empty($orders)) {
                        $fail('订单不存在');
                        return;
                    }
                    if ($orders->is_comment == 1) {
                        $fail('订单已经评价');
                        return;
                    }
                    if ($orders->status != 3) {
                        $fail('订单暂时无法评价');
                        return;
                    }
                }
            ],
        ];
    }

    public function messages()
    {
        return [
            'orders_id.required' => '请选择需要评论的订单',
            'comment_info.required' => '请输入评论内容',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            "code"  => 500,
            "msg"   => $validator->getMessageBag()->first(),
            "data"  => null,
        ], 200, [
            'Content-Type'=>'application/json; charset=UTF-8'
        ]));
    }
}
